import { defineStore } from "pinia";
import axios from "axios";
import { nanoid } from "nanoid";

// export const useTalkStore = defineStore('talk', {
//     actions: {
//         async getATalk() {
//             // 连续结构赋值加重命名
//             let {data:{content:title}} = await axios.get('http://api.uomg.com/api/rand.qinghua?format=json')
//             let obj = {id: nanoid(), title: title}
//             this.talkList.unshift(obj)
//         }
//     },
//     state() {
//         return {
//             // talkList: [
//             //     { id: '01', title: 'ttt01' },
//             //     { id: '02', title: 'ttt02' },
//             //     { id: '03', title: 'ttt03' },
//             //     { id: '04', title: 'ttt04' }
//             // ]
//             talkList: JSON.parse(localStorage.getItem('talkList') as string) || []
//         }
//     }
// })

import { reactive } from "vue";
export const useTalkStore = defineStore('talk', () => {
    const talkList = reactive(
        JSON.parse(localStorage.getItem('talkList') as string) || []
    )

    async function getATalk() {
        // 连续结构赋值加重命名
        let {data:{content:title}} = await axios.get('http://api.uomg.com/api/rand.qinghua?format=json')
        let obj = {id: nanoid(), title: title}
        talkList.unshift(obj)
    }

    return {talkList, getATalk}
})